//
//  COHomeListCollectionViewCell.swift
//  CocoProject
//
//  Created by Ruthless on 2023/8/19.
//

import UIKit

class COHomeListCollectionViewCell: UICollectionViewCell {
    
    var model: COHomeListModel? {
        didSet {
            guard let model = model else { return }
            
            imageView.kf.setImage(with: URL(string: model.photo)) { result in
                switch result {
                case let .success(result):
                    let size = result.image.size
                    model.cellSize = size
                case .failure(_):
                    break
                }
            }
            
            avatar.kf.setImage(with: URL(string: model.sculpture))
            nameLabel.text = model.nickName
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func configUI() {
        backgroundColor = .rgba(17, 7, 41, 1)
        contentView.addSubview(imageView)
        contentView.addSubview(markView)
        contentView.addSubview(avatar)
        contentView.addSubview(nameLabel)
        
        imageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        avatar.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.bottom.equalToSuperview().offset(-10)
            make.width.height.equalTo(30)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(avatar.snp.right).offset(5)
            make.centerY.equalTo(avatar)
            make.right.equalToSuperview().offset(-10)
        }
        
        markView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(100)
        }
    }
    
    private lazy var imageView: UIImageView = {
        let imgV = UIImageView()
        imgV.layer.masksToBounds = true
        imgV.layer.cornerRadius = 15
        
        imgV.kf.setImage(with: URL(string: "https://cdn1.liuchuo.net/20190118012251.png"))
        return imgV
    }()
    
    private lazy var avatar: UIImageView = {
        let imgV = UIImageView()
        imgV.layer.masksToBounds = true
        imgV.layer.cornerRadius = 15
        
        imgV.kf.setImage(with: URL(string: "https://cdn1.liuchuo.net/20190118012251.png"))
        return imgV
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(15)
        label.textColor = .white
        label.text = "dsdasdklsld"
        return label
    }()
    
    private lazy var markView: UIView = {
        let view = UIView()
        let bgLayer1 = CAGradientLayer()
        bgLayer1.colors = [UIColor.rgba(0, 0, 0, 0).cgColor, UIColor.rgba(0, 0, 0, 0.5).cgColor]
        bgLayer1.locations = [0, 1]
        bgLayer1.frame = CGRect(x: 0, y: 0, width: (SCREENWIDTH-24)/2, height: 100)
        bgLayer1.startPoint = CGPoint(x: 0, y: 0)
        bgLayer1.endPoint = CGPoint(x: 0, y: 1)
        view.layer.insertSublayer(bgLayer1, at: 1)
        return view
    }()
}
